import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 22479
 * Date: 2022-11-29
 * Time: 21:35
 */
@WebServlet("/userInfo")
public class UserInfoServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //这个方法用于查看作者/用户详情信息

        //blogId 用于判断当前是博客列表页还是博客详情页
        String blogId = req.getParameter("blogId");

        if(blogId == null) {
            //blogId为空 当前是博客列表页 显示用户详细信息
            getUserInfoFromSession(req, resp);

        } else {
            //当前是博客作者详情信息
            getUserInfoFromDB(req, resp, Integer.parseInt(blogId));
        }
    }

    private void getUserInfoFromDB(HttpServletRequest req, HttpServletResponse resp, int blogId) throws IOException {
        ObjectMapper objectMapper = new ObjectMapper();
        //1.先根据blogId查询对象的博客
        BlogDao blogDao = new BlogDao();
        Blog blog = blogDao.selectOne(blogId);

        if(blog == null) {
            //如果blogId在数据库中没有
            resp.setStatus(404);
            resp.setContentType("text/html;charset=utf8");
            resp.getWriter().write("当前博客不存在");
            return;
        }

        //2.根据对应blog对象查询对象user
        UserDao userDao = new UserDao();
        User user = userDao.selectById(blog.getUserId());

        if(user == null) {
            //如果blogId在数据库中没有
            resp.setStatus(404);
            resp.setContentType("text/html;charset=utf8");
            resp.getWriter().write("当前博客不存在");
            return;
        }

        //3.返回对象user信息

        //将密码设为空,明文传输不合适
        user.setPassword("");
        resp.setContentType("application/json;charset=utf8");
        resp.getWriter().write(objectMapper.writeValueAsString(user));


    }

    private void getUserInfoFromSession(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        ObjectMapper objectMapper = new ObjectMapper();
        //当前是博客列表页 显示用户详细信息
        //1.获取到session
        HttpSession session = req.getSession(false);
        if(session == null) {
            //未登录就访问
            resp.setStatus(403);
            resp.setContentType("text/html;charset=utf8");
            resp.getWriter().write("当前未登录");
            return;
        }

        User user = (User) session.getAttribute("user");
        if(user == null) {
            //当前user对象为空 没有对应用户信息
            resp.setStatus(403);
            resp.setContentType("text/html;charset=utf8");
            resp.getWriter().write("当前未登录");
            return;
        }
        //将密码设为空,明文传输不合适
        user.setPassword("");
        resp.setContentType("application/json;charset=utf8");
        resp.getWriter().write(objectMapper.writeValueAsString(user));

    }


}
